/**
 * 获取商品列表数据
 */
function getProductList() {
	// ajax({
	// 	method: 'get',
	// 	url: 'http://10.7.162.150:8089/api/shop/list',
	// 	success: function (res) {
	// 		showProductList(res.resultInfo.list)
	// 	},
	// })

	// myPromise({
	// 	method:'get',
	// 	url:'http://10.7.162.150:8089/api/shop/list',
	// }).then(res=>{
	// 	showProductList(res.resultInfo.list)
	// })

	// 使用axios库发起网络请求
	axios({
		method: 'get',
		url: 'http://10.7.162.150:8089/api/shop/list',
	}).then(res => {
		console.log(res.data)
		res = res.data
		showProductList(res.resultInfo.list)
	})
}
/**
 * 动态渲染商品列表
 */
function showProductList(productList) {
	let list = productList.map(item => {
		return `<div class="product-item" onclick="onDetail(${item.id})">
                    <img src="${item.picture}" alt="pic1">
                    <p>${item.product}</p>
                    <p>￥${item.price}  ${item.putaway}人已买</p>
                </div>`
	})
	const rootEle = document.querySelector('.container')
	rootEle.innerHTML = list.join('')
}

/**
 * 跳转详情页
 * @param {*} id
 */
function onDetail(id) {
	// 跳转详情页
	location.href = './detail.html?id=' + id
}

/**
 * 注册跳转
 */
function bindSign() {
	const sign = document.querySelector('.sign')
	sign.addEventListener('click', function () {
		location.href = './sign.html'
	})
}

/**
 * 登录
 */
function bindLogin() {
	const login = document.querySelector('.login')
	const loginForm = document.querySelector('.login_wraper')
	const closeEle = document.querySelector('.login_wraper .close')
	const userInput = document.querySelector('input[name="username"]')
	const passwordInput = document.querySelector('input[name="password"]')

	login.addEventListener('click', function () {
		loginForm.style.display = 'block'
	})

	closeEle.addEventListener('click', function () {
		loginForm.style.display = 'none'
	})

	loginForm.addEventListener('submit', function (e) {
		e = e || window.event
		e.preventDefault() // 阻止默认行为

		let username = userInput.value
		let password = passwordInput.value

		myPromise({
			method: 'post',
			url: 'http://10.7.162.150:8089/api/member/login',
			data: {
				username,
				password,
			},
		}).then(res => {
			// 登录成功
			if (res.resultCode == 1) {
				let user = res.resultInfo
				showUser(user)
				// 保存用户状态信息
				let userInfo = {
					username:user.username,
					nick:user.nick,
					id:user.uid,
					headerimg:user.headerimg
				}
				Cookies.set('user', JSON.stringify(userInfo),{expires:1})
			} else {
				alert('用户名或密码出错')
			}
			loginForm.style.display = 'none' // 关闭登录界面
		})
	})
}

/**
 * 判断用户是否登录，如果登录显示个人中心
 * @param {*} user 
 */
function showUserMessage(){
    let user = Cookies.get('user')
	if(user){
		showUser(JSON.parse(user))
	}
}

/**
 * 显示个人中心
 */
function showUser(user) {
	let str = `<div class="logo">
				<img src="${user.headerimg}" alt="">
				<p>昵称:${user.username}</p>
				<button onclick="onLogout()">注销</button>
			</div>`
	const header = document.querySelector('.header')
	header.innerHTML = str
}

function onLogout() {
	// 注销清除cookie
	Cookies.remove('user')
	
	let str = `  <div class="wraper">
					<button class="sign">注册</button>
					<button class="login">登录</button>
				</div>`
	const header = document.querySelector('.header')
	header.innerHTML = str

	
}

getProductList()
bindSign()
bindLogin()
showUserMessage()
